<template>
  <el-dialog v-model="drawer" @close="closeDialog" title="批量修改" width="600px">
    <el-form :model="formData" size="small" :rules="rules" ref="formRef" label-width="100px">
      <el-form-item label="客户阶段" prop="stageId">
        <el-select v-model="formData.stageId" placeholder="请选择客户阶段" size="small">
          <el-option v-for="item in stageList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button size="small" @click="drawer = false">取消</el-button>
      <el-button type="primary" size="small" :loading="submitLoad" @click="submit(formRef)">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import {ref, toRefs, computed, reactive} from 'vue'
import {ElMessage} from 'element-plus'
import opportunityApi from '/@/api/ums/customer/opportunity'
import stageApi from '/@/api/ums/customer/opportunityStage'

export default {
  name: 'CustomerBatchEdit',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    opportunityIds: {
      type: Array,
      default: []
    }
  },
  emits: ['callback', 'update:dialogVisible'],
  setup(props, context) {
    const formRef = ref()
    const {dialogVisible, opportunityIds} = toRefs(props)
    let drawer = computed({
      get: () => {
        if (dialogVisible.value) {
          state.formData.opportunityIds = opportunityIds.value
          stageApi.getList().then(res => state.stageList = res.data || []).catch(e => console.log(e))
        }
        return dialogVisible.value
      },
      set: val => context.emit('update:dialogVisible', val)
    })
    const state = reactive({
      formData: {
        opportunityIds: null,
        stageId: null
      },
      rules: {
        stageId: {required: true, message: '请选择阶段', trigger: 'change'}
      },
      stageList: [],
      submitLoad: false
    })

    const submit = (formEl) => {
      formEl.validate(async (valid) => {
        if (!valid) {
          return
        }
        state.submitLoad = true
        try {
          const res = await opportunityApi.batchEdit(state.formData)
          if (res.data) {
            state.submitLoad = false
            ElMessage.success('修改成功！')
            context.emit('update:dialogVisible', false)
          }
        } catch (e) {
          console.log(e)
          state.submitLoad = false
        }
      })
    }
    const closeDialog = () => {
      context.emit('callback')
    }

    return {
      ...toRefs(state),
      formRef,
      drawer,
      closeDialog,
      submit
    }
  }
}

</script>

<style scoped lang="scss">
</style>


